package com.iqi.onigao.module.friend;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.iqi.onigao.constant.SessionConstant;
import com.iqi.onigao.context.Bean;
import com.iqi.onigao.module.player.PlayerBO;
import com.iqi.onigao.util.RandomUtil;

import net.IClient;
import net.Package;
import si.client.CFriend;
import si.pod.FriendPOD;
import si.pod.PlayerPOD;
@Bean(FriendBO.class)
public class FriendBOImpl extends CFriend implements FriendBO {

	@Override
	public boolean removeFriendsResult(IClient session, Package p, int code) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean applyFriendsResult(IClient session, Package p, int code) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean dealWithApplyResult(IClient session, Package p, int code) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean addBlacklistResult(IClient session, Package p, int code) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean removeBlacklistResult(IClient session, Package p, int code) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean searchPlayerResult(IClient session, Package p, int code, FriendPOD friend) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean setRemarkResult(IClient session, Package p, int code, FriendPOD friend) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean recommendResult(IClient session, Package p, int code, List<FriendPOD> friends) {
		session.setAttr(SessionConstant.EXEC, new Runnable() {
			@Override
			public void run() {
				// 发送申请
				List<String> ids = new ArrayList<>();
				for (FriendPOD friendPOD : friends) {
					if (RandomUtil.randomBoolean()) {
						ids.add(friendPOD.getPId());
					}
				}
				CFriend.applyFriends(session, ids);
			}
		});
		
			
		return false;
	}

	@Override
	public boolean notifyAddFriend(IClient session, Package p, List<FriendPOD> friends) {
		Map<Long, FriendPOD> map =  session.getAttr(SessionConstant.FRIENDS);
		for (FriendPOD friendPOD : friends) {
			map.put(friendPOD.getId(), friendPOD);
		}
		return false;
	}

	@Override
	public boolean notifyDeleteFriend(IClient session, Package p, List<Long> ids) {
		Map<Long, FriendPOD> map =  session.getAttr(SessionConstant.FRIENDS);
		map.keySet().removeAll(ids);
		return false;
	}

	@Override
	public boolean notifyUpdateFriend(IClient session, Package p, List<FriendPOD> friends) {
		Map<Long, FriendPOD> map =  session.getAttr(SessionConstant.FRIENDS);
		for (FriendPOD friendPOD : friends) {
			try {
				map.put(friendPOD.getId(), friendPOD);	
			} catch (Exception e) {
				e.printStackTrace();
				System.exit(0);
			}
			
		}
		return false;
	}

}
